﻿Ext.define('SistIntegral.RRHH.Frmcostosistemadepensiones', {

    alias: "Frmcostosistemadepensiones",
    forward: true,

    Formulario: function (btn) {

        var ComboSistemaPensiones = new SistIntegral.Componentes.Objetos().ComboSistemaPension();

        var StoreEstDelInvE = Ext.create('Ext.data.Store', {
            model: 'CostoSistemadePensiones',
            proxy: {
                type: 'ajax',
                url: 'costosistemadepensiones/Index',
                reader: { type: 'json', root: 'grid' }
            }
        });

        StoreEstDelInvE.on('load', function (store, records) {
            Ext.getCmp('FrmCostosistemadepensiones').loadRecord(records[0]);

            Ext.getCmp('Combosistemapension').getStore().loadRecords(records);
            Ext.getCmp('Combosistemapension').setValue(records[0].get('IdSistemaPensiones'));


        });

        var Frm = Ext.create('Ext.form.Panel', {
            id: 'FrmCostosistemadepensiones',
            bodyPadding: '5',
            fieldDefaults: {
                msgTarget: 'side',
                labelAlign: 'right',
                labelWidth: 100,
                anchor: '30%'
                //margin: '5 5 5 5'
            },
            items: [{
                xtype: 'container',
                layout: 'hbox',
                margin: '0 0 0 0',
                items: [{
                    xtype: 'fieldset',
                    border: 0,
                    flex: 1,
                    items: [{
                        xtype: 'fieldset',
                        flex: 1,
                        title: 'Datos Generales',
                        layout: 'anchor',
                        defaults: {
                            //                            anchor: '100%',
                            hideEmptyLabel: false
                        },
                        items: [
                            ComboSistemaPensiones,
                            {
                                xtype: 'numberfield',
                                name: 'Fondo',
                                fieldLabel: 'Fondo:',
                                format: '0.00%',
                                allowBlank: false,
                                width: 90,
                                minValue: 0,
                                maxValue: 100
                            },
                            {
                                xtype: 'numberfield',
                                name: 'comision',
                                fieldLabel: 'Comision:',
                                format: '0.00%',
                                allowBlank: false,
                                width: 90,
                                minValue: 0,
                                maxValue: 100
                            },
                            {
                                xtype: 'numberfield',
                                name: 'comisionmixta',
                                fieldLabel: 'Comision Mixta 2:',
                                format: '0.00%',
                                allowBlank: false,
                                width: 90,
                                minValue: 0,
                                maxValue: 100
                            },
                            {
                                xtype: 'numberfield',
                                name: 'seguro',
                                fieldLabel: 'Seguro:',
                                format: '0.00%',
                                allowBlank: false,
                                width: 90,
                                minValue: 0,
                                maxValue: 100
                            },
                            {
                                xtype: 'numberfield',
                                name: 'topeseguro',
                                fieldLabel: 'Tope Seguro:',
                                format: 'S/ 0.00',
                                allowBlank: false,
                                width: 90,
                                minValue: 0,
                                maxValue: 10000
                            }
                           ]
                    }]
                },
                ]
            }],

            buttons: [{
                text: 'Guardar', iconCls: 'guardar',
                handler: function () {
                    var form = this.up('form').getForm();
                    if (form.isValid()) {
                        form.submit({
                            submitEmptyText: false,
                            url: btn.hrefTarget + (btn.itemId == 'edit' ? btn.Seleccion.get('Id') : ''),
                            //                            url: rec.get('hrefTarget') + '/' + combo.getValue() + '/' + (cmbtipotra.getValue() ? cmbtipotra.getValue() : 3),
                            success: function (form, action) {
                                console.log(action);
                                console.log(form);
                                Ext.getCmp('gridCostosistemadepensiones').getStore().load();
                                Ext.getCmp('WCostosistemadepensiones').close();
                                Ext.MessageBox.show({
                                    msg: 'Registro Creado Correctamente...',
                                    progressText: 'Actualizando...',
                                    width: 300,
                                    icon: 'ext-mb-download'
                                });
                                setTimeout(function () {
                                    Ext.MessageBox.hide();
                                }, 1000);
                            },
                            failure: function (form, action) {
                                //...
                            }
                        });
                    }
                }
            }, {
                text: 'Limpiar', iconCls: 'limpiar',
                handler: function () {
                    this.up('form').getForm().reset();
                }
            }]
        });
        if (btn.itemId == 'edit') { StoreEstDelInvE.load(); }
        return Frm;
    },

    Window: function (btn) {
        var Seleccion;
        //alert("ok");
        //Ext.Ajax.request({
        //    url: 'contratoadenda/ComprobarSueldo/'+btn.IdPersonal,
        //    success: function (httpReq) {
        //        var textRes = httpReq.responseText;
        //        var vector = textRes.split("#");
        //        if (vector[0] != '0') {
        //        btn.sueldo = vector[0];
        //        //btn.idejerciciomes = vector[2];
        //        }
        //    }
        //});

        if (btn.itemId == 'edit') {
            Seleccion = Ext.getCmp('images-view').getSelectionModel().getSelection()[0];
            if (!Seleccion) {
                Ext.Msg.show({
                    title: 'Advertencia',
                    msg: 'Seleccione Persona a Editar',
                    icon: Ext.MessageBox.WARNING,
                    buttons: Ext.Msg.OK
                });
                return false;
            }
            else {
                btn.Seleccion = Seleccion
            }
        }

        var Item = this.Formulario(btn);

        var win = Ext.create('widget.window', {
            id: 'WCostosistemadepensiones',
            title: btn.getText() + ' Costo Sistema de Pensiones',
            closable: true,
            //autoScroll:true,
            //bodyPadding:'5',
            border: false,
            width: 700,
            minWidth: 600,
            height: 300,
            layout: 'fit',
            modal: true,
            items: [Item]
        });

        return win.show();
    }
});

   